﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.MathUtils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Base;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmServiceItemAdd : FormBase
    {
        public ControlClassModeEnum Flag { get; set; }

        private DBEntityServiceItem _serviceItem;

        public frmServiceItemAdd()
        {
            InitializeComponent();
        }

        public frmServiceItemAdd(DBEntityServiceItem serviceItem)
        {
            InitializeComponent();
            this._serviceItem = serviceItem;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool FormValidate()
        {
            string error = "";
            if (txtCode.Text.Trim() == string.Empty)
            {
                error = "请输入项目编码";
                MsgBox.MsgInfo(error);
                txtCode.Focus();
                SetErrorControl(txtCode, error);
                return false;
            }

            if (txtName.Text.Trim() == string.Empty)
            {
                error = "请输入项目名称。";
                MsgBox.MsgInfo(error);
                txtName.Focus();
                SetErrorControl(txtName, error);
                return false;
            }

            if (txtSpanTimes.Text.Trim() == string.Empty)
            {
                error = "请输入服务时间。";
                MsgBox.MsgInfo(error);
                txtSpanTimes.Focus();
                SetErrorControl(txtSpanTimes, error);
                return false;
            }
            if (!NumberUtil.IsNumber(txtSpanTimes.Text.Trim()))
            {
                error = "服务时间应为整数，请重新输入。";
                MsgBox.MsgInfo(error);
                txtSpanTimes.Focus();
                SetErrorControl(txtSpanTimes, error);
                return false;
            }

            if (txtIndexCode.Text.Trim() == string.Empty)
            {
                error = "请输入快速定位码。";
                MsgBox.MsgInfo(error);
                txtIndexCode.Focus();
                SetErrorControl(txtIndexCode, error);
                return false;
            }

            if (txtPrice.Text.Trim() == string.Empty)
            {
                error = "请输入项目价格。";
                MsgBox.MsgInfo(error);
                txtPrice.Focus();
                SetErrorControl(txtPrice, error);
                return false;
            }

            if (!NumberUtil.IsNonNegativeDecimal(txtPrice.Text.Trim()))
            {
                error = "项目价格应为数字，请重新输入。";
                MsgBox.MsgInfo(error);
                txtPrice.Focus();
                SetErrorControl(txtPrice, error);
                return false;
            }
            
            if (cbeStatus.SelectedIndex < 0)
            {
                error = "请选择项目状态。";
                MsgBox.MsgInfo(error);
                cbeStatus.Focus();
                SetErrorControl(cbeStatus, error);
                return false;
            }

            if (txtCount.Text.Trim() == string.Empty)
            {
                error = "请输入项目次数。";
                MsgBox.MsgInfo(error);
                txtCount.Focus();
                SetErrorControl(txtCount, error);
                return false;
            }

            string sCount = txtCount.Text.Trim();

            if (sCount != "-1" && !NumberUtil.IsNonNegativeNumber(txtCount.Text.Trim()))
            {
                error = "项目次数应为正整数，请重新输入。";
                MsgBox.MsgInfo(error);
                txtCount.Focus();
                SetErrorControl(txtCount, error);
                return false;
            }


            if (txtAvailableDays.Text.Trim() == string.Empty)
            {
                error = "请输入有效服务天数。";
                MsgBox.MsgInfo(error);
                txtAvailableDays.Focus();
                SetErrorControl(txtAvailableDays, error);
                return false;
            }
            if (!NumberUtil.IsDecimal(txtAvailableDays.Text.Trim()) || Convert.ToInt16(txtAvailableDays.Text.Trim()) < -1)
            {
                error = "有效服务天数应为正整数或-1，请重新输入。";
                MsgBox.MsgInfo(error);
                txtAvailableDays.Focus();
                
                SetErrorControl(txtAvailableDays, error);
                return false;
            }
            
            if (dtiStartDate.Text.Trim() == string.Empty)
            {
                error = "请选择开始时间。";
                MsgBox.MsgInfo(error);
                dtiStartDate.Focus();
                SetErrorControl(dtiStartDate, error);
                return false;
            }
            if (dtiEndDate.Text.Trim() == string.Empty)
            {
                error = "请选择结束时间。";
                MsgBox.MsgInfo(error);
                dtiEndDate.Focus();
                SetErrorControl(dtiEndDate, error);
                return false;
            }
            if (dtiStartDate.Value > dtiEndDate.Value)
            {
                error = "开始时间不允许大于结束时间。";
                MsgBox.MsgInfo(error);
                dtiStartDate.Focus();
                SetErrorControl(dtiStartDate, error);
                return false;
            }

            if (chkCallBackFlag.Checked)
            {
                if (txtCallBackDays.Text.Trim() == string.Empty)
                {
                    error = "请输入回访时间。";
                    MsgBox.MsgInfo(error);
                    txtCallBackDays.Focus();
                    SetErrorControl(txtCallBackDays, error);
                    return false;
                }
            }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (FormValidate() == false)
            {
                return;
            }

            _serviceItem.SNAME = txtName.Text;
            _serviceItem.SPRICE = Convert.ToDecimal(txtPrice.Text.Trim());
            _serviceItem.STIMESPAN = Convert.ToInt32(txtSpanTimes.Text.Trim());
            string sState = "";
            if (cbeStatus.SelectedIndex == 0)
                sState = cbeStatus.Items[0].ToString();
            else
                sState = cbeStatus.Items[1].ToString();
            _serviceItem.STATE = DBEntityServiceItem.ENUM_STATE.ChineseStrToEnumStr(sState);
            _serviceItem.SAVAILABLEDAYS = Convert.ToInt32(txtAvailableDays.Text.Trim());
            _serviceItem.SINDEXCODE = txtIndexCode.Text.Trim();
            _serviceItem.STARTDATE = dtiStartDate.Value;
            _serviceItem.SENDDATE = dtiEndDate.Value;
            _serviceItem.SREMARK = txtMemo.Text.Trim();
            _serviceItem.SERVICEITEMCODE = txtCode.Text.Trim();
            _serviceItem.SCALLBACKFLAG = chkCallBackFlag.Checked ? 1 : 0;
            _serviceItem.SCOUNT = Convert.ToInt32(txtCount.Text);
            if (chkCallBackFlag.Checked)
                _serviceItem.SCALLBACKDAY = Convert.ToInt32(txtCallBackDays.Text.Trim());
            else
                _serviceItem.SCALLBACKDAY = 0;

            bool bReturn = false;
            string error = string.Empty;
            if (Flag == ControlClassModeEnum.emInsert)
            {
                bReturn = BussinessProxyManager.Instance.InsertServiceItem(_serviceItem, out error);
            }
            else if (Flag == ControlClassModeEnum.emEdit)
            {
                bReturn = BussinessProxyManager.Instance.UpdateServiceItem(_serviceItem, out error);
            }

            if (bReturn == false)
            {
                MsgBox.MsgErr(error);
                return;
            }
            MsgBox.MsgInfo("保存成功。");
            this.DialogResult = DialogResult.OK;
        }

        private void SetTitle()
        {
            if (Flag == ControlClassModeEnum.emInsert)
            {
                this.Text = "新增服务项目";
            }
            else if (Flag == ControlClassModeEnum.emEdit)
            {
                this.Text = "修改服务项目";
            }
        }

        private void frmServiceItemAdd_Shown(object sender, EventArgs e)
        {
            SetTitle();
            cbeStatus.SelectedIndex = 0;
            if (Flag == ControlClassModeEnum.emEdit)
            {
                txtCode.Text = _serviceItem.SERVICEITEMCODE;
                txtName.Text = _serviceItem.SNAME;
                txtPrice.Text = _serviceItem.SPRICE.ToString();
                txtSpanTimes.Text = _serviceItem.STIMESPAN.ToString();
                cbeStatus.Text = DBEntityServiceItem.ENUM_STATE.EnumStrToChineseStr(_serviceItem.STATE);
                txtAvailableDays.Text = _serviceItem.SAVAILABLEDAYS.ToString();
                txtIndexCode.Text = _serviceItem.SINDEXCODE;
                dtiStartDate.Text = _serviceItem.STARTDATE.ToString();
                dtiEndDate.Text = _serviceItem.SENDDATE.ToString();
                txtMemo.Text = _serviceItem.SREMARK;
                chkCallBackFlag.Checked = (_serviceItem.SCALLBACKFLAG == 1) ? true : false;
                txtCallBackDays.Text = _serviceItem.SCALLBACKDAY.ToString();
                txtCount.Text = _serviceItem.SCOUNT.ToString();
            }
            chkCallBackFlag_CheckedChanged(null, null);
        }

        private void chkCallBackFlag_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCallBackFlag.Checked)
                txtCallBackDays.Enabled = true;
            else
                txtCallBackDays.Enabled = false;
        }
    }
}
